package util

import (
	"gobase/app/model"
	"strconv"

	"github.com/casbin/casbin"
)

//casbin_rule policy管理
//给用户添加多个角色
func AddRolesForUser(ce *casbin.Enforcer, userid int64, roleids []string) {
	idStr := strconv.FormatInt(userid, 10)
	ce.DeleteRolesForUser(idStr)
	for _, roleid := range roleids {
		ce.AddRoleForUser(idStr, roleid)
	}
}

//角色添加权限
func AddFuncsForRole(ce *casbin.Enforcer, roleid int, resourceList []*model.Resource) {
	roleidStr := strconv.Itoa(roleid)
	for _, r := range resourceList {
		path := r.Url + "*"
		if path == "#" {
			continue
		}
		method := "*"
		ce.AddPolicy(roleidStr, path, method)
	}
}
